package com.salon.modules.job.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.salon.modules.makeup.entity.ReservationEntity;
import com.salon.modules.makeup.service.ReservationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 演示测试定时任务 testTask为spring bean的名称
 */
@Component("testTask")
public class TestTask implements ITask {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ReservationService reservationService;

    @Override
    public void run(String params) {
        logger.info("预约服务已过期, 取消预约服务 start");
        LambdaQueryWrapper<ReservationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReservationEntity::getStatus, 0);
        List<ReservationEntity> serviceList = reservationService.list(queryWrapper);
        List<ReservationEntity> cancelServiceList = new ArrayList<>();
        for (ReservationEntity reservationEntity : serviceList) {
            Date reservationDate = reservationEntity.getReservationDate();
            if(reservationDate.before(new Date())) {
                ReservationEntity entity = new ReservationEntity();
                entity.setId(reservationEntity.getId());
                entity.setStatus(2);
                cancelServiceList.add(entity);
            }
        }
        reservationService.updateBatchById(cancelServiceList);
        logger.info("预约服务已过期, 取消预约服务 end");
    }

}
